﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class crm_salienceMember_UploadPage : System.Web.UI.Page
{

    private DataTable getTableInfo(string sql)
    {
        DataSet data = new DataSet();
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            try
            {
                sda.SelectCommand = new SqlCommand();
                sda.SelectCommand.CommandText = sql;
                sda.SelectCommand.Connection = new SqlConnection(emp.SqlServerDAL.SQLHelper.CONN_STRING_NON_DTC);
                sda.Fill(data);
            }
            catch { }
        }
        return data.Tables[0];
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string path = "";
            string filename = "";
            if (!string.IsNullOrEmpty(Request.Params["dr"]) && !string.IsNullOrEmpty(Request.Params["IRB_UID"]))
            {
                string sql = "SELECT  IRB_UID      ,IRB_XLS_FILE      ,IRB_STATUS      ,IRB_UPLOAD_NUM      ,IRB_UPLOAD_USER      ,IRB_UOLOAD_DATE      ,IRB_REMARK ,(select [user_name] from [SYSTEM_USER_TBL] where [user_uid]=IRB_UPLOAD_USER) as IRB_DOLOAD_USER_NAME   FROM  INDIVIDUAL_RESPONSE_BATCH   WHERE IRB_UID='" + Request.Params["IRB_UID"].ToString() + "'";
                System.Data.DataTable dt1 = getTableInfo(sql);
                if (dt1.Rows.Count>0)
                {
                    if (dt1.Rows[0]["IRB_STATUS"].ToString() == "处理完成")
                    {
                        path = HttpContext.Current.Server.MapPath("excel/") + dt1.Rows[0]["IRB_XLS_FILE"].ToString();
                        filename = dt1.Rows[0]["IRB_XLS_FILE"].ToString();
                    }
                    else 
                    {
                        path = HttpContext.Current.Server.MapPath("excel/") + "cw"+dt1.Rows[0]["IRB_XLS_FILE"].ToString();
                        filename = "cw" + dt1.Rows[0]["IRB_XLS_FILE"].ToString();
                    }
                    
                }
                

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
                if (fileInfo.Exists == false)
                {
                    Response.Write("<script>alert('下载文件数据为空!');</script>");
                    Response.Write("<script language=javascript>window.history.go(-1);</script>");
                    return;
                }
                Response.Clear();
                Response.ContentType = "application/x-excel";
                Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
                Response.WriteFile(path);
                
            }
            else
            {
                return;
            }

            
            Response.End();
        }
    }
}
